#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ECUReset_0x11_test.py
@Time    :   2024/08/15 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pickle import TRUE
from pytest_assume.plugin import assume
import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True


ecuReset_NRC13 = [('11', '7F 11 13'),
                  ('11 01 01', '7F 11 13'),
                  ('11 01 25 33', '7F 11 13'),
                  ('11 7E 01', '7F 11 13'),
                  ('11 81 01', '7F 11 13'),
                  ('11 83 01', '7F 11 13'),
                  ('11 84 01', '7F 11 13'),
                  ('11 04 11', '7F 11 13'),
                  ('11 03 11', '7F 11 13'),
                  ('11 80 01', '7F 11 13')
                  ]

ecuReset_NRC12 = [('11 02', '7F 11 12'),
                  ('11 03', '7F 11 12'),
                  ('11 04', '7F 11 12'),
                  ('11 05', '7F 11 12'),
                  ('11 06', '7F 11 12'),
                  ('11 07', '7F 11 12'),
                  ('11 08', '7F 11 12'),
                  ('11 09', '7F 11 12'),
                  ('11 82', '7F 11 12'),
                  ('11 83', '7F 11 12'),
                  ('11 84', '7F 11 12'),
                  ('11 85', '7F 11 12'),
                  ('11 86', '7F 11 12'),
                  ('11 87', '7F 11 12')
                  ]

ecuReset_NRC7F = [('11 01', '7F 11 7F'),
                  ('11 81', '7F 11 7F')
                  ]


class Test_ECUReset_Phy(object):

    @allure.severity('normal')
    @allure.story('ECU SID 0x11 ECU Reset Positive response')
    @allure.description('Test positive response of 0x11')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ecuReset_PosResp_Extended_Phy(self, action, Driver):
        iStep = 1
        # 扩展模式1101 积极响应
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1101', '5101', 3, iStep, True)

    @allure.severity('normal')
    @allure.story('ECU SID 0x11 ECU Reset Positive response')
    @allure.description('Test positive response of 0x11')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ecuReset_PosResp_Programming_Phy(self, action, Driver):
        iStep = 1
        # 编程模式1101 积极响应
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1101', '5101', 2, iStep, True)

    @allure.severity('normal')
    @allure.story('ECU SID 0x11 ECU Reset Positive response')
    @allure.description('Test positive response of 0x11')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ecuReset_SuppressPosMsg_Extended_Phy(self, action, Driver):
        iStep = 1
        # 扩展模式1101 积极响应
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1181', '7F1112', 3, iStep, True)

    @allure.severity('normal')
    @allure.story('ECU SID 0x11 ECU Reset Positive response')
    @allure.description('Test positive response of 0x11')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ecuReset_SuppressPosMsg_Programming_Phy(self, action, Driver):
        iStep = 1
        # 编程模式1101 积极响应
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1181', '7F1112', 2, iStep, True)

    @allure.severity('normal')
    @allure.story('Test NRC33 of SID 0x11')
    @allure.description('Test NRC33 of SID 0x11 with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ecuReset_NRC33_Extended_Phy(self, action, Driver):
        iStep = 1
        # 扩展模式1101 NRC33
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1101', '7F1133', 3, iStep, False)

    @allure.severity('normal')
    @allure.story('Test NRC33 of SID 0x11')
    @allure.description('Test NRC33 of SID 0x11 with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ecuReset_NRC33_Programming_Phy(self, action, Driver):
        iStep = 1
        # 编程模式1101 NRC33
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1101', '7F1133', 2, iStep, False)

    # @allure.severity('normal')
    # @allure.story('Test NRC7E of SID 0x11')
    # @allure.description('Test NRC7E of SID 0x11 in programming session with physical addressing')
    # @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    # def test_ecuReset_NRC7E_Programming_Phy(self, action, Driver):
    #     iStep = 1
    #     iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1102', '7F117E', 2, iStep, True)
    
    # @allure.severity('normal')
    # @allure.story('Test NRC7E of SID 0x11')
    # @allure.description('Test NRC7E of SID 0x11 in extended session with physical addressing')
    # @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    # def test_ecuReset_NRC7E_Extended_Phy(self, action, Driver):
    #     iStep = 1
    #     iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1102', '7F117E', 3, iStep, True)

    @allure.severity('normal')
    @allure.story('Test NRC22 of SID 0x11')
    @allure.description('Test NRC22 of SID 0x11 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ecuReset_NRC22_Extended_Phy(self, action, Driver):
        iStep = 1
        time.sleep(2)
        # 进入扩展模式保持,并完成安全认证
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 3, iStep, TRUE)
        # 设置车速>3kph
        Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 80)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1101', '7F1122', 0, iStep, False)
        # 设置车速>3kph
        Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 0)

    # 20220913 系统工程师确认进入编程模式，不再接收应用报文
    @allure.severity('normal')
    @allure.story('Test NRC22 of SID 0x11')
    @allure.description('Test NRC22 of SID 0x11 in programming session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ecuReset_NRC22_Programming_Phy(self, action, Driver):
        iStep = 1
        # 进入扩展模式保持,并完成安全认证
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 2, iStep, True)

        # 设置车速>3kph
        Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 80)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1101', '7F1122', 0, iStep, False)
        # 设置车速<3kph
        Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 0)


    ids = [f'{req}' for req, resp in ecuReset_NRC12]
    @allure.severity('minor')
    @allure.description('Test NRC12 of 0x11 in programming session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', ecuReset_NRC12, ids=ids)
    def test_RoutineControl_NRC12_Programming_Phy(self, action, Driver, req, resp):
        time.sleep(0.1)
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)

    ids = [f'{req}' for req, resp in ecuReset_NRC12]
    @allure.severity('minor')
    @allure.description('Test NRC12 of 0x11 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', ecuReset_NRC12, ids=ids)
    def test_RoutineControl_NRC12_Extended_Phy(self, action, Driver, req, resp):
        time.sleep(0.5)
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)

    ids = [f'{req}' for req, resp in ecuReset_NRC13]

    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x11 in programming session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', ecuReset_NRC13, ids=ids)
    def test_RoutineControl_NRC13_Programming_Phy(self, action, Driver, req, resp):
        time.sleep(0.1)
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)

    ids = [f'{req}' for req, resp in ecuReset_NRC13]
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x11 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', ecuReset_NRC13, ids=ids)
    def test_RoutineControl_NRC13_Extended_Phy(self, action, Driver, req, resp):
        time.sleep(0.1)
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)
    
    
    ids = [f'{req}' for req, resp in ecuReset_NRC7F]
    @allure.severity('normal')
    @allure.story('Test NRC7F of SID 0x11')
    @allure.description('Test NRC7F of SID 0x11 with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', ecuReset_NRC7F, ids=ids)
    def test_ecuReset_NRC7F_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

